package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.api.http.model.request.SearchProductHttpRequest;
import com.meituan.catering.management.product.biz.model.request.SearchProductBizRequest;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/5/19 14:31
 */
public class SearchProductBizRequestConverter {
    public static SearchProductBizRequest toSearchProductBizRequest(SearchProductHttpRequest searchProductHttpRequest) {
        if (Objects.isNull(searchProductHttpRequest)) {
            return null;
        }
        SearchProductBizRequest searchProductBizRequest = new SearchProductBizRequest();
        searchProductBizRequest.setPageIndex(searchProductHttpRequest.getPageIndex());
        searchProductBizRequest.setPageSize(searchProductHttpRequest.getPageSize());
        searchProductBizRequest.setName(searchProductHttpRequest.getName());
        searchProductBizRequest.setUnitOfMeasure(searchProductHttpRequest.getUnitOfMeasure());
        searchProductBizRequest.setEnabled(searchProductHttpRequest.getEnabled());
        searchProductBizRequest.setFrom(searchProductHttpRequest.getFrom());
        searchProductBizRequest.setTo(searchProductHttpRequest.getTo());
        searchProductBizRequest.setSortFields(toSortField(searchProductHttpRequest.getSortFields()));
        return searchProductBizRequest;
    }

    public static List<SearchProductBizRequest.SortField> toSortField(List<SearchProductHttpRequest.SortField> fieldList) {
        if (CollectionUtils.isEmpty(fieldList)) {
            return Collections.emptyList();
        }
        List<SearchProductBizRequest.SortField> sortFieldList = Lists.newLinkedList();
        for (SearchProductHttpRequest.SortField sortField : fieldList) {
            SearchProductBizRequest.SortField field = new SearchProductBizRequest.SortField();
            field.setField(sortField.getField());
            field.setAsc(sortField.getAsc());
            sortFieldList.add(field);
        }
        return sortFieldList;
    }
}
